package services;

import models.pubobj.AniObjectType;
import models.pubobj.*;
import services.exception.*;
import utils.AniGeneralUtils;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * User: yeh
 * Date: 2/23/13
 * Time: 5:59 PM
 * For:  wilddragon
 */
public class AniPublicPropService {
    public static AniObjectState getObjectStateWithIdOrName(Object stateIdOrName){
        AniObjectState oneState = null;
        if(stateIdOrName instanceof Long){
            oneState = AniObjectState.findById(Long.valueOf((Long)stateIdOrName));
        }else if(stateIdOrName instanceof String){
            oneState = AniObjectState.findByStateKey(String.valueOf(stateIdOrName));
        }else{
            throw new AniDataException("PARAMETER_TYPE_COULD_ONLY_BE_LONG_OR_STRING");
        }
        if(oneState == null){
            throw new AniRuleException("STATE_NOT_EXISTS");
        }
        return oneState;
    }
    public static Map<Integer, String> getObjectStatesName(){
        List<AniObjectState> objectStates = AniObjectState.findAll();
        Map<Integer, String> objectStatesNameMap = new LinkedHashMap<Integer, String>();
        for(AniObjectState oneState: objectStates){
            objectStatesNameMap.put(oneState.id.intValue(), oneState.stateKey);
        }
        return objectStatesNameMap;
    }
    public static AniHttpMethod getHttpMethodWithIdOrName(Object httpMethodIdOrName){
        AniHttpMethod oneMethod = null;
        if(httpMethodIdOrName instanceof Long){
            oneMethod = AniHttpMethod.findById((Long)httpMethodIdOrName);
        }else if(httpMethodIdOrName instanceof String){
            oneMethod = AniHttpMethod.findByHttpMethodKey(String.valueOf(httpMethodIdOrName));
        }else{
            throw new AniDataException("PARAMETER_TYPE_COULD_ONLY_BE_LONG_OR_STRING");
        }
        if(oneMethod == null){
            throw new AniRuleException("HTTP_METHOD_NOT_EXISTS");
        }
        return oneMethod;
    }
    public static Map<Integer, String> getHttpMethodsName(){
        List<AniHttpMethod> httpMethods = AniHttpMethod.findAll();
        Map<Integer, String> httpMethodsNameMap = new LinkedHashMap<Integer, String>();
        for(AniHttpMethod oneMethod: httpMethods){
            httpMethodsNameMap.put(
                    oneMethod.id.intValue(),
                    oneMethod.httpMethodKey
            );
        }
        return httpMethodsNameMap;
    }
    public static AniObjectType getObjectTypeWithIdOrName(Object objectTypeIdOrName){
        AniObjectType oneObjType = null;
        if(objectTypeIdOrName instanceof Long){
            oneObjType = AniObjectType.findById((Long)objectTypeIdOrName);
        }else if(objectTypeIdOrName instanceof String){
            oneObjType = AniObjectType.findByTypeKey(String.valueOf(objectTypeIdOrName));
        }else{
            throw new AniDataException("PARAMETER_TYPE_COULD_ONLY_BE_LONG_OR_STRING");
        }
        if(oneObjType == null){
            throw new AniRuleException("OBJECT_TYPE_NOT_EXISTS");
        }
        return oneObjType;
    }
    public static Map<Integer, String> getObjectTypesName(){
        List<AniObjectType> aniObjectTypes = AniObjectType.findAll();
        Map<Integer, String> types = new HashMap<Integer, String>();
        if(AniGeneralUtils.isListEmpty(aniObjectTypes)){
            return null;
        }
        for(AniObjectType oneType: aniObjectTypes){
            types.put(oneType.id.intValue(), oneType.typeKey);
        }
        return types;
    }
}
